package com.play.ballen.home.ui

import android.content.Context
import android.content.Intent
import android.view.View
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.ActivityPlayCountBinding
import com.play.ballen.home.entity.PlayCountInfo
import com.play.ballen.home.task.TaskUtil
import com.play.ballen.home.vm.PlayCountVM
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.util.DateFormat
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.component.viewmodel.BaseViewModel.EmptyVM
import com.yuven.baselib.utils.ResourceUtil

class PlayCountActivity : BaseStdTitleActivity<PlayCountVM, ActivityPlayCountBinding>() {

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, PlayCountActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun viewModelClass(): Class<PlayCountVM> {
        return PlayCountVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_play_count
    }

    override fun onToolbarRightIconClick() {
        PlayCountRecordActivity.start(this)
    }

    override fun onToolbarRightTvClick() {
        PlayCountRecordActivity.start(this)
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.actionUploadStepSuccess.observe(this) {
            TaskUtil.complete(TaskUtil.COMPLETE_BY_MANUAL)
            finish()
        }
    }

    override fun initData() {
        if (TaskUtil.getPlayCountInfo() == null) {
            finish()
            ToastUtils.show("playCountInfo can not be null")
            return
        }

        val rightIcon = getTitleBarLayout().getRightIcon()
        rightIcon?.visibility = View.VISIBLE
        rightIcon?.setImageResource(R.mipmap.ic_me_recharge_detail)
        getTitleBarLayout().getRightTextView()?.visibility = View.VISIBLE
        getTitleBarLayout().setRightTitle(ResourceUtil.getString(R.string.play_record_title))

        TaskUtil.stepChangeCallBack = { leftCount ->
            dataBinding.tvPlayCount.text = "${TaskUtil.getStepCount()}"
            dataBinding.tvPlayCountLeft.text = "$leftCount"
        }
        TaskUtil.timeChangeCallBack = { formatTime ->
            dataBinding.tvTime.text = formatTime
        }

        TaskUtil.getPlayCountInfo()?.run {
            dataBinding.tvPlayCount.text = "${TaskUtil.getStepCount()}"
            //format time
            //dataBinding.tvTime.text = DateFormat.secToDayTime2(time)

            dataBinding.tvPlayCountLeft.text = "${TaskUtil.getLeftCount()}"
        }

        dataBinding.tvSubmit.setOnSingleClickListener {
            viewModel.taskSubmit(TaskUtil.getStepCount())
        }
    }

    override fun initIntent(intent: Intent?) {

    }

    override fun getToolBarTitle(): String = ""

    override fun onDestroy() {
        super.onDestroy()
        TaskUtil.stepChangeCallBack = null
        TaskUtil.timeChangeCallBack = null
    }
}